﻿using SIG.Basic.Config;
using SIG.Basic.Configs;
using SIG.Model.Extensions;
using SIG.Model.ViewModel.Back;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Xml.Linq;

namespace SIG.WebUI.Areas.Admin.Controllers
{
   
    [Authorize]
    public class InfoController : BaseController
    {
        //
        // GET: /bbi_Admin/Info/
        [Authorize(Roles = "创始人,管理员")]
        public ViewResult Index()
        {
            var cfe = SettingsManager.Contact;
            CompanyInfoVM contactInfo = new CompanyInfoVM {
                CompanyName = cfe.CompanyName,
                CompanyShortName = cfe.CompanyShortName,
                Address = cfe.Address,
                Coordinate = cfe.Coordinate,
                ContactMan = cfe.ContactMan,
                Fax = cfe.Fax,
                Phone = cfe.Phone,
                ZipCode = cfe.ZipCode,
                Mobile = cfe.Mobile,
                MailTo = cfe.MailTo,
                MailCC = cfe.MailCC,
           };           

            return View(contactInfo);
        }

         [Authorize(Roles = "创始人,管理员")]
        [HttpPost]
        public ActionResult Index(CompanyInfoVM vm)
        {
            if (!ModelState.IsValid)
            {
                AR.SetError();
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            

            try
            {
                var xmlFile = Server.MapPath("~/Config/ContactSettings.config");
                XDocument doc = XDocument.Load(xmlFile);

                var item = doc.Descendants("Settings").FirstOrDefault();
                item.Element("companyName").SetValue(vm.CompanyName);
                item.Element("companyShortName").SetValue(vm.CompanyShortName??"");
                item.Element("address").SetValue(vm.Address);
                item.Element("coordinate").SetValue(vm.Coordinate??"");
                item.Element("contactMan").SetValue(vm.ContactMan);
                item.Element("fax").SetValue(vm.Fax??"");
                item.Element("phone").SetValue(vm.Phone);
                item.Element("zipCode").SetValue(vm.ZipCode??"");
                item.Element("mobile").SetValue(vm.Mobile??"");
                item.Element("mailTo").SetValue(vm.MailTo);
                item.Element("mailCC").SetValue(vm.MailCC??"");                  
                doc.Save(xmlFile);

                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            catch (Exception ex)
            {
                AR.SetError(ex.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
                
                //TempData["message"] = "保存成功！";          
           
        }

        // GET: /bbi_Admin/Social/
         [Authorize(Roles = "创始人,管理员")]
        public ViewResult Social()
        {
            var sle = SettingsManager.Social;

            SocialVM Social = new SocialVM {
               Oicq = sle.Oicq,
                OicqTwo = sle.OicqTwo,
                SinaWeibo = sle.SinaWeibo,
                WeiXing = sle.WeiXing,
                WeiXingCode = sle.WeiXingCode
            };

            return View(Social);
        }

         [Authorize(Roles = "创始人,管理员")]
        [HttpPost]
        public JsonResult Social(SocialVM vm)
        {
            if (!ModelState.IsValid)
            {
                AR.SetError();
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
         
            try
            {
                var xmlFile = Server.MapPath("~/Config/SocialSettings.config");
                XDocument doc = XDocument.Load(xmlFile);

                var item = doc.Descendants("Settings").FirstOrDefault();
                item.Element("Oicq").SetValue(vm.Oicq??"");
                item.Element("OicqTwo").SetValue(vm.OicqTwo ?? "");
                item.Element("SinaWeibo").SetValue(vm.SinaWeibo??"");
                item.Element("WeiXing").SetValue(vm.WeiXing ?? "");
                item.Element("WeiXingCode").SetValue(vm.WeiXingCode??"");
           
                doc.Save(xmlFile);

                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            catch (Exception ex)
            {
                AR.SetError(ex.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            

        }

        // GET: /bbi_Admin/Info/SiteInfo
       [Authorize(Roles = "创始人,管理员")]
        public ViewResult SiteInfo()
        {
            var info = SettingsManager.Site;
            SiteInfoVM siteInfoVM = new SiteInfoVM{
                SiteName = info.SiteName,
                SiteDomainName = info.SiteDomainName,
                WebNumber = info.WebNumber,
                BaiduSiteID = info.BaiduSiteID
            };          
            return View(siteInfoVM);
        }

         [Authorize(Roles = "创始人,管理员")]
        [HttpPost]
        public JsonResult SiteInfo(SiteInfoVM vm)
        {
            if (!ModelState.IsValid)
            {
                AR.SetError();
                return Json(AR, JsonRequestBehavior.DenyGet); 
            }

            try
            {
                var xmlFile = Server.MapPath("~/Config/GlobalSettings.config");
                XDocument doc = XDocument.Load(xmlFile);

                var item = doc.Descendants("Settings").FirstOrDefault();
                item.Element("siteName").SetValue(vm.SiteName??"");
                item.Element("siteDomainName").SetValue(vm.SiteDomainName??"");
                item.Element("webNumber").SetValue(vm.WebNumber??"");
                item.Element("baiduSiteID").SetValue(vm.BaiduSiteID??"");
                doc.Save(xmlFile);
             
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
             catch(Exception ex)
            {
                AR.SetError(ex.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
           
        }


        // GET: /bbi_Admin/Info/EmailSet     邮箱服务设置
        [Authorize(Roles = "创始人,管理员")]
        public ViewResult EmailSet()
        {
            var smtp = SettingsManager.SMTP;
            EmailSetVM emailSetVM = new EmailSetVM
            {
                From = smtp.From,
                SmtpServer = smtp.SmtpServer,
                Port = smtp.Port,
                UserName = smtp.UserName,
                Password = smtp.Password,
                EnableSsl = smtp.EnableSsl
            };
            return View(emailSetVM);
        }


        [Authorize(Roles = "创始人,管理员")]
        [HttpPost]
        public JsonResult EmailSet(EmailSetVM vm)
        {
            if (!ModelState.IsValid)
            {
                AR.SetError();
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            try
            {
                var xmlFile = Server.MapPath("~/Config/SMTPSettings.config");
                XDocument doc = XDocument.Load(xmlFile);
                var item = doc.Descendants("Settings").FirstOrDefault();

                item.Element("From").SetValue(vm.From ?? "");
                item.Element("SmtpServer").SetValue(vm.SmtpServer ?? "");
                item.Element("Port").SetValue(vm.Port);
                item.Element("UserName").SetValue(vm.UserName ?? "");

                if (!string.IsNullOrEmpty(vm.Password))
                {
                    item.Element("Password").SetValue(vm.Password);
                }
              
                item.Element("EnableSsl").SetValue(vm.EnableSsl);

                doc.Save(xmlFile);

                return Json(AR, JsonRequestBehavior.DenyGet);
        }
            catch (Exception ex)
            {
                AR.SetError(ex.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
    }

}

    }
}
